﻿using Kang.Api.Dtos;
using Kang.ExtendMethod;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Annotations;
using System.Text;

namespace DakaPathApp.Controllers
{
    [ApiController]
    public class TestController : ControllerBase
    {
        /// <summary>
        /// 测试接口，用于测试服务是否正常运行
        /// </summary>
        /// <returns></returns>
        [SwaggerOperation(
            Summary = "测试",
            Description = "接口测试",
            Tags = new[] { "测试相关接口" }
        )]
        [HttpGet, Route("test"), Produces("application/json")]
        [SwaggerResponse(StatusCodes.Status200OK, "查询成功", typeof(ResponseDto), "application/json")]
        public IActionResult TestGet([SwaggerParameter("姓名")] string name)
        {
            return Content(JsonConvert.SerializeObject(new ResponseDto($"Hello, {name}!")), "application/json", Encoding.UTF8);
        }

        /// <summary>
        /// 测试接口，用于测试服务是否正常运行
        /// </summary>
        /// <returns></returns>
        [SwaggerOperation(
            Summary = "测试",
            Description = "接口测试",
            Tags = new[] { "测试相关接口" }
        )]
        [HttpPost, Route("test"), Produces("application/json"), Consumes("application/json")]
        [SwaggerResponse(StatusCodes.Status200OK, "创建成功", typeof(ResponseDto), "application/json")]
        public IActionResult TestPost([SwaggerRequestBody("回写参数")] TestAddDto arg)
        {
            if (arg == null)
            {
                return Content(JsonConvert.SerializeObject(new ResponseDto(1, "未传递参数")), "application/json", Encoding.UTF8);
            }
            if (arg.Data == null)
            {
                return Content(JsonConvert.SerializeObject(new ResponseDto(1, "未传递数据对象参数")), "application/json", Encoding.UTF8);
            }
            if (arg.Data.Name.IsBlank())
            {
                return Content(JsonConvert.SerializeObject(new ResponseDto(1, "数据对象无Name参数")), "application/json", Encoding.UTF8);
            }
            if (arg.Data.Phone.IsBlank())
            {
                return Content(JsonConvert.SerializeObject(new ResponseDto(1, "数据对象无联系方式参数")), "application/json", Encoding.UTF8);
            }
            return Content(JsonConvert.SerializeObject(new ResponseDto($"Hello, {arg.Data.Name}, Phone: {arg.Data.Phone}!")), "application/json", Encoding.UTF8);
        }
    }
}
